package m.jclecai.com.view.klpk;

import android.content.Context;
import android.view.View;

import java.util.List;

import butterknife.BindViews;
import m.jclecai.com.R;
import m.jclecai.com.pop.BasePopupWindow;

/**
 * Created by andy on 2017/6/14.
 */

public class KlpkBarPopupWindow extends BasePopupWindow implements PokerCategory.OnCheckedChangeListener{


    @BindViews({R.id.category_pairs,R.id.category_same_color,R.id.category_flush,
            R.id.category_straight_flush,R.id.category_three_sheets,
            R.id.category_one,R.id.category_two,R.id.category_three,
            R.id.category_four,R.id.category_five,R.id.category_fix
    })
    List<PokerCategory> mPokerCategories;

    private int mCurrentCategory;

    public KlpkBarPopupWindow(Context context) {
        super(context);
        init();
    }

    private void init() {
        //这里的话，我们做一个初始化的一个操作；
        setFocusable(true);
        setOutsideTouchable(true);
        setContentView(R.layout.klpk_popup_window_layout);

        initPokerCategory(mPokerCategories.get(0),"猜对子","最高奖88元");
        initPokerCategory(mPokerCategories.get(1),"猜同花","最高奖90元");
        initPokerCategory(mPokerCategories.get(2),"猜顺子","最高奖400元");

        initPokerCategory(mPokerCategories.get(3),"猜同花顺","最高奖2150元");
        initPokerCategory(mPokerCategories.get(4),"猜豹子","最高奖6400元");

        initPokerCategory(mPokerCategories.get(5),"任选一","奖金5元");
        initPokerCategory(mPokerCategories.get(6),"任选二","奖金33元");
        initPokerCategory(mPokerCategories.get(7),"任选三","奖金116元");

        initPokerCategory(mPokerCategories.get(8),"任选四","奖金46元");
        initPokerCategory(mPokerCategories.get(9),"任选五","奖金22元");
        initPokerCategory(mPokerCategories.get(10),"任选六","奖金12元");

        for (PokerCategory pokerCategory : mPokerCategories) {
            pokerCategory.setOnCheckedChangeListener(this);
        }

        mCurrentCategory=0;
        initPokerCategoryStatus();

    }


    @Override
    protected boolean allowBackgroundTranslucent() {
        return true;
    }

    @Override
    protected int setPopWindowAnimationStyle() {
        return -1;
    }

    public void initPokerCategory(PokerCategory pokerCategory,String title, String subTitle){
        pokerCategory.setTitle(title);
        pokerCategory.setSubTitle(subTitle);
    }

    @Override
    public void isChecked(PokerCategory pokerCategory, boolean isChecked) {
        for (PokerCategory category : mPokerCategories) {
            category.setChecked(false);
        }

        pokerCategory.setChecked(isChecked);

        //更新当前选中的位置；
        mCurrentCategory=mPokerCategories.indexOf(pokerCategory);

        if (mOnCategoryChangeListener != null) {
            //在这里我们要做的一个操作就是改变title的值；
            mOnCategoryChangeListener.OnCategoryChange(mCurrentCategory);
        }
        //将PopupWindow消失；
        dismiss();
    }

    /**
     * 显示显示popupWindow里面的内容；
     * @param dropDwonView
     */
    public void show(View dropDwonView){
        showAsDropDown(dropDwonView);
    }

    public void setPager(int position){
        mCurrentCategory=position;
        initPokerCategoryStatus();
    }


    /**
     * 根据当前的选中值，设置相应的状态；
     */
    private void initPokerCategoryStatus() {
        for (PokerCategory category : mPokerCategories) {
            category.setChecked(false);
            //如果当前对象正好等于选中的索引值的话，就设置为选中的状态；
            if (category==mPokerCategories.get(mCurrentCategory)) {
                category.setChecked(true);
            }
        }
    }

    public OnCategoryChangeListener mOnCategoryChangeListener;

    public void setOnCategoryChangeListener(OnCategoryChangeListener onCategoryChangeListener) {
        mOnCategoryChangeListener = onCategoryChangeListener;
    }

    public interface OnCategoryChangeListener {
        /**
         * 类别改变的回掉的方法
         * @param position  类别目标的位置；
         */
        public void OnCategoryChange(int position);
    }
}
